/**
 * Quick object check - this is primarily used to tell
 * Objects from primitive values when we know the value
 * is a JSON-compliant type.
 */
export const isObject = function (obj) {
  return obj !== null && typeof obj === 'object'
}

/**
 * Check if two values are loosely equal - that is,
 * if they are plain objects, do they have the same shape?
 */
export const looseEqual = function (a, b) {
  // eslint-disable-next-line eqeqeq
  return a == b || (
    isObject(a) && isObject(b) ? JSON.stringify(a) === JSON.stringify(b) : false
  )
}

/**
 * Check if a val exists in arr using looseEqual comparison
 */
export const looseIndexOf = function (arr, val) {
  for (let i = 0; i < arr.length; i++) {
    if (looseEqual(arr[i], val)) {
      return i
    }
  }

  return -1
}

/**
 * Check if the given string starts with the query, beginning
 * at the given position
 */
export const startsWith = function (string, query, position = 0) {
  return string.substr(position, query.length) === query
}
